package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.dao.WxCommentMapper;
import com.cskaoyan.catmall.model.bo.WxCommentPostBo;
import com.cskaoyan.catmall.model.pojo.MarketUser;
import com.cskaoyan.catmall.model.vo.*;
import com.cskaoyan.catmall.service.WxCommentService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.crypto.SecretKey;
import java.util.List;

/**
 * 创建日期: 2022-01-10-14-22
 * author : XGQ
 * description: WxCommentService的实现类
 */
@Service
@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
public class WxCommentServiceImpl implements WxCommentService {

    @Autowired
    WxCommentMapper wxCommentMapper;

    /**
     * @description
     * @author XGQ
     * @date 2022/01/10 16:27
     * @param
     * @param valueId 商品id
     * @param type 评论类型，如果type=0，则是商品评论；如果是type=1，则是专题评论
     * @param limit 一页最多项
     * @param page 第几页
     * @param showType 1是有图，0是全部
     * @return com.cskaoyan.catmall.model.vo.CommentListVo
     */
    @Override
    public CommentListVo getCommentList(Integer valueId, Integer type, Integer limit, Integer page, Integer showType) {
        Integer start = (page - 1) * limit;
        //获取list数组
        ListVo[] list = wxCommentMapper.getListById(valueId,type,showType,start,limit);
        //total个数
        Integer total = wxCommentMapper.getNumByValueIdAndType(valueId,type);
        for (ListVo listVo : list) {
            //获取userInfo
            UserInfoVo userInfo = wxCommentMapper.getUserById(listVo.getUserId());
            listVo.setUserInfo(userInfo);
        }
        //总共页数
        Integer pages = total / limit + 1;
        //包装
        CommentListVo commentListVo = new CommentListVo();
        commentListVo.setLimit(limit);
        commentListVo.setPage(page);
        commentListVo.setPages(pages);
        commentListVo.setTotal(total);
        commentListVo.setList(list);

        return commentListVo;
    }

    /**
     * @description 全部评论
     * @author XGQ
     * @date 2022/01/10 23:58
     * @param
     * @param valueId
     * @param type
     * @return com.cskaoyan.catmall.model.vo.WxCountVo
     */
    @Override
    public WxCountVo getCommentCount(Integer valueId, Integer type) {
        // 全部专题评论信息
        List<Integer> pictureCounts = wxCommentMapper.getPictureCountByIdAndType(valueId,type);
        // 全部专题评论条数
        Integer allCount = pictureCounts.size();
        // 有图专题评论条数
        Integer hasPictureCount = 0;
        for (Integer pictureCount : pictureCounts) {
            if (pictureCount == 1){
                hasPictureCount++;
            }
        }
        // 包装
        WxCountVo wxCountVo = new WxCountVo();
        wxCountVo.setAllCount(allCount);
        wxCountVo.setHasPicCount(hasPictureCount);
        return wxCountVo;
    }
    /**
     * @description 评论提交
     * @author XGQ
     * @date 2022/01/11 14:14
     * @param
     * @param wxCommentPostBo
     * @return com.cskaoyan.catmall.model.vo.WxCommentPostVo
     */
    @Override
    public WxCommentPostVo getCommentPost(WxCommentPostBo wxCommentPostBo) {
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        wxCommentPostBo.setUserId(user.getId());
        // 存入信息并获取自增id
        wxCommentMapper.saveComment(wxCommentPostBo);
        // 根据自增id获取对应信息
        WxCommentPostVo wxCommentPostVo = wxCommentMapper.getInfo(wxCommentPostBo.getId());
        return wxCommentPostVo;
    }
}
